function getCordovaFile () {
  return cordova.file
}

function getResolveLocalFileSystemURL () {
  return window.resolveLocalFileSystemURL
}

function getStorageDirectory () {
  let storageDir = cordova.file.applicationStorageDirectory
  return storageDir
}

function checkIfFileExists (path, successCallback, fileDoesNotExist) {
  window.resolveLocalFileSystemURL(path, function (fileEntry) {
    if (fileEntry) {
      console.log('File exists:', fileEntry.name)
      successCallback(fileEntry)
    } else {
      fileDoesNotExist()
    }
  }, fileDoesNotExist)
}

function moveTo (fileEntry, targetPath, newFileName, successCallback, errorCallback) {
  let originalName = fileEntry.name
  let url = fileEntry.toURL()
  if (fileEntry && fileEntry.isFile) {
    console.log('file name', originalName, 'fullPath=', url, 'will be move to', targetPath)
    window.resolveLocalFileSystemURL(targetPath, function (directory) {
      let isDir = directory.isDirectory
      if (isDir) {
        console.log('target directory path:', directory.toURL(), newFileName)
        fileEntry.moveTo(directory, newFileName, successCallback, errorCallback)
        // fileEntry.copyTo(directory, newFileName, successCallback, errorCallback)
      }
    }, errorCallback)
  } else {
    console.log(url, 'is not a file')
  }
}

function remove (targetPath, successCallback, errorCallback) {
  window.resolveLocalFileSystemURL(targetPath, function (directory) {
    console.log('target directory will be deleted:', directory.toURL())
    let isDir = directory.isDirectory
    if (isDir) {
      directory.removeRecursively(successCallback, errorCallback)
    } else {
      directory.remove(successCallback, errorCallback)
    }
  }, errorCallback)
}

function listFile (path, successCallback, errorCallback) {
  console.log('list file path', path)
  window.resolveLocalFileSystemURL(path, function (fileEntry) {
    if (fileEntry) {
      let isDir = fileEntry.isDirectory
      console.log('file type isDirectory?', isDir)
      if (isDir) {
        let reader = fileEntry.createReader()
        reader.readEntries(successCallback, errorCallback)
      } else {
        return fileEntry
      }
    } else {
      errorCallback()
    }
  }, errorCallback)
}

function createFile (target, fileName, content) {
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    console.log('file system open: ' + fs.name)
    let rootPath = target
    console.log('storage root path', rootPath)
    fs.root.getFile(fileName, {create: true, exclusive: false}, function (fileEntry) {
      console.log('fileEntry is file?' + fileEntry.isFile.toString())
      // fileEntry.name == 'someFile.txt'
      // fileEntry.fullPath == '/someFile.txt'
      writeFile(fileEntry, content)
    })
  })
}

function writeFile (fileEntry, dataObj, onWriteSucc, onWriteError) {
  // Create a FileWriter object for our FileEntry (log.txt).
  fileEntry.createWriter(function (fileWriter) {
    fileWriter.onwriteend = function () {
      console.log('Successful file write...')
      if (onWriteSucc) {
        onWriteSucc()
      }
    }

    fileWriter.onerror = function (e) {
      console.log('Failed file write: ' + e.toString())
      if (onWriteError) {
        onWriteError()
      }
    }

    // If data object is not passed in,
    // create a new Blob instead.
    if (!dataObj) {
      dataObj = new Blob(['some file data'], {type: 'text/plain'})
    }

    fileWriter.write(dataObj)
  })
}

function readFile (fileEntry, successCallback, onErrorReadFile) {
  fileEntry.file(function (file) {
    let reader = new FileReader()
    reader.onloadend = function () {
      if (successCallback) {
        successCallback(this.result)
      }
    }
    reader.readAsText(file)
  }, onErrorReadFile)
}

export {
  getStorageDirectory, getCordovaFile, listFile, checkIfFileExists, createFile, moveTo, remove, readFile
}
